package cn.itsource.pay.service.impl;

import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.pay.contants.AliPayConfig;
import cn.itsource.pay.domain.AliPayInfo;
import cn.itsource.pay.domain.PayBill;
import cn.itsource.pay.mapper.PayBillMapper;
import cn.itsource.pay.service.IAliPayInfoService;
import cn.itsource.pay.service.IPayBillService;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PayBillServiceImpl extends BaseServiceImpl<PayBill> implements IPayBillService{

    @Autowired
    private PayBillMapper mapper;
    @Autowired
    private IAliPayInfoService aliPayInfoService;

    /*
        1.系统会有多个支付渠道
        2.根据用户选择的不同渠道 调用不同的支付方法
     */
    @Override
    public String goPay(PayBill bill,String orderSn) {
        //根据店铺查询支付信息
        AliPayInfo aliPayInfo = aliPayInfoService.queryById(bill.getShop().getId());

        switch (bill.getPayChannel()){
            case 1://走支付宝的支付
               return alipay(aliPayInfo,bill,orderSn);
            case 2: //走微信

                break;
            case  3://走银联支付

                break;

        }





        return null;
    }

    @Override
    public PayBill queryByOrderSn(String out_trade_no) {
        return mapper.queryByOrderSn(out_trade_no);
    }

    //支付的支付
    private String  alipay(AliPayInfo aliPayInfo,PayBill bill,String orderSn) {
        try {
            //获得初始化的AlipayClient
            AlipayClient alipayClient = new DefaultAlipayClient(AliPayConfig.gatewayUrl,
                    aliPayInfo.getAppid(),
                    aliPayInfo.getMerchant_private_key(),
                    "json", AliPayConfig.charset,
                    aliPayInfo.getAlipay_public_key(), AliPayConfig.sign_type);
            //设置请求参数
            AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
            alipayRequest.setReturnUrl(AliPayConfig.return_url);
            alipayRequest.setNotifyUrl(AliPayConfig.notify_url);
            //商户订单号，商户网站订单系统中唯一订单号，必填
            String out_trade_no =orderSn ;//new String(request.getParameter("WIDout_trade_no").getBytes("ISO-8859-1"),"UTF-8");
            //付款金额，必填
            String total_amount =bill.getMoney().toString(); //new String(request.getParameter("WIDtotal_amount").getBytes("ISO-8859-1"),"UTF-8");
            //订单名称，必填
            String subject =bill.getDigest(); // new String(request.getParameter("WIDsubject").getBytes("ISO-8859-1"),"UTF-8");
            //商品描述，可空
            String body =bill.getDigest(); //new String(request.getParameter("WIDbody").getBytes("ISO-8859-1"),"UTF-8");
            alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                    + "\"total_amount\":\""+ total_amount +"\","
                    + "\"subject\":\""+ subject +"\","
                    + "\"body\":\""+ body +"\","
                    + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
            //请求
            String result = alipayClient.pageExecute(alipayRequest).getBody();

            return result;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
